package com.guochaojava.api;

import com.guochaojava.model.Member;
import com.guochaojava.model.Record;
import com.guochaojava.model.enums.MemberStatusEnum;
import com.guochaojava.param.GameEndParam;
import com.guochaojava.service.MemberService;
import com.guochaojava.service.RecordService;
import com.guochaojava.util.Result;
import com.lgd.winter.wechat.core.WeChatTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 比赛相关接口
 *
 * @author guochao
 * @since 1.0.0
 */
@Api(tags = "比赛相关接口")
@Validated
@RestController
@RequestMapping("/game")
public class GameApi {

    @Autowired
    private MemberService memberService;
    @Autowired
    private RecordService recordService;


    @ApiOperation(value = "扫一扫")
    @PostMapping("/scan")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "二维码扫描获取的参数", dataType = "int", paramType = "query")
    })
    public Object scan(@NotNull Integer id) {
        try {
            Member member = memberService.selectByPrimaryKey(id);
    /*        if(MemberStatusEnum.PLAYING.getCode().equals(member.getStatus())){
                return Result.buildError();
            }*/
            member.setStatus(MemberStatusEnum.PLAYING.getCode());
            memberService.updateByPrimaryKeySelective(member);
            return Result.buildOK(member);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "发起比赛")
    @PostMapping("/start")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "用户openId", dataType = "string", paramType = "query")
    })
    public Object start(@NotNull String openId) {
        try {
//            Member m = memberService.selectByOpenId(openId);
//
            Member member = memberService.start(openId);
            return Result.buildOK(member);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "结束比赛")
    @PostMapping("/end")
    public Object end(@Valid GameEndParam param) {
        try {
            recordService.insertSelective(param);
            return Result.buildOK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "确认比赛")
    @PostMapping("/affirm")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "formId", value = "消息推送formId", dataType = "string", paramType = "query")
    })
    public Object affirm(@NotNull Integer id, @NotNull String formId) {
        try {
            recordService.affirm(id, formId);
            return Result.buildOK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }

    @ApiOperation(value = "分享比赛")
    @PostMapping("/share")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录id", dataType = "int", paramType = "query")
    })
    public Object share(@NotNull Integer id) {
        try {
            Record record = recordService.selectRivalDetailById(id);
            return Result.buildOK(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.buildError();
        }
    }
}